import sys

private_hub = sys.argv[1]
private_hub_auth = sys.argv[2]


registry_config = """
        [plugins."io.containerd.grpc.v1.cri".registry.configs."{private_hub}".tls]
          insecure_skip_verify = true
        [plugins."io.containerd.grpc.v1.cri".registry.configs."{private_hub}".auth]
          username = ""
          password = ""
          auth = "{private_hub_auth}"
          identitytoken = ""
""".format(private_hub=private_hub, private_hub_auth=private_hub_auth)


registry_searchline = '[plugins."io.containerd.grpc.v1.cri".registry.configs]'
rs = ''

with open('/etc/containerd/config.toml', 'r') as f:
    lines = f.readlines()
    for i, line in enumerate(lines):
        if registry_searchline in line:
            lines.insert(i+1, registry_config)
            rs = ''.join(lines)
            break

with open('/etc/containerd/config.toml', 'w') as f:
    f.write(rs)


mirrors_config = """
        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
          endpoint = ["https://pooj3a7i.mirror.aliyuncs.com"]
         [plugins."io.containerd.grpc.v1.cri".registry.mirrors."{private_hub}"]
          endpoint = ["http://{private_hub}"]
""".format(private_hub=private_hub)


mirrors_searchline = '[plugins."io.containerd.grpc.v1.cri".registry.mirrors]'
ms = ''


with open('/etc/containerd/config.toml', 'r') as f:
    lines = f.readlines()
    for i, line in enumerate(lines):
        if mirrors_searchline in line:
            lines.insert(i+1, mirrors_config)
            ms = ''.join(lines)
            break

with open('/etc/containerd/config.toml', 'w') as f:
    f.write(ms)
